package com.workhours.controller;

import com.workhours.dto.TripStartRequest;
import com.workhours.service.UserService;
import com.workhours.vo.Result;
import com.workhours.vo.UserStatusResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户控制器
 *
 * @author WorkHours
 * @since 2025-09-19
 */
@Tag(name = "用户管理", description = "用户状态和出差时间管理相关接口")
@RestController
@RequestMapping("/api/user")
public class UserController {
    
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    
    @Autowired
    private UserService userService;
    
    /**
     * 获取用户状态
     *
     * @return 用户状态信息
     */
    @Operation(summary = "获取用户状态", description = "获取当前用户的出差开始日期和所有休息记录")
    @GetMapping("/status")
    public Result<UserStatusResponse> getUserStatus() {
        try {
            log.info("收到获取用户状态请求");
            UserStatusResponse response = userService.getUserStatus();
            log.info("用户状态获取成功: {}", response);
            return Result.success("获取成功", response);
        } catch (Exception e) {
            log.error("获取用户状态失败", e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }
    
    /**
     * 设置出差开始日期
     *
     * @param request 出差开始日期请求
     * @return 设置结果
     */
    @Operation(summary = "设置出差开始日期", description = "设置当前用户的出差开始日期")
    @PostMapping("/trip-start")
    public Result<?> setTripStartDate(@Valid @RequestBody TripStartRequest request) {
        try {
            log.info("收到设置出差开始日期请求: {}", request);
            userService.setTripStartDate(request);
            log.info("出差开始日期设置成功");
            return Result.success("设置成功");
        } catch (Exception e) {
            log.error("设置出差开始日期失败", e);
            return Result.error("设置失败：" + e.getMessage());
        }
    }
}
